package com.gupaoedu.vip.proxy.jdk;

import sun.misc.ProxyGenerator;

import java.io.FileOutputStream;
import java.io.IOException;

/**
 * Created by zhaozhonghai on 2017/5/22.
 */
//测试代理模式
public class TestProxy {
    public static void main(String[] args){
        XiaoMing xm=new XiaoMing();
        LianJia lj=new LianJia();
        Person proxy= (Person) lj.getInstance(xm);
        proxy.zuFangZi();

        //获取生成的代理class
        byte[] bytes=ProxyGenerator.generateProxyClass("111",new Class[]{proxy.getClass()});
        //生成的class 名称可以是任意的
        //生成的class 包含接口中的所有方法
        FileOutputStream fos=null;
        try {
            fos=new FileOutputStream("proxy11.class");
            fos.write(bytes);
            fos.flush();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                if(fos!=null){
                    fos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
